package org.dev.vlp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.dev.vlp.bean.JsonResponse;
import org.dev.vlp.constants.Roles;
import org.dev.vlp.interceptor.RequiredPermission;
import org.dev.vlp.service.WordCollectionService;
import org.dev.vlp.service.WordService;
import org.dev.vlp.vo.WordDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * Description
 * Param
 * return
 * Author:86184
 * Date: 2023/1/16
 */
@RestController
@RequestMapping("/learn/word")
@RequiredPermission({Roles.ROLE_USER})
public class LearnWordController extends BaseController {

    @Autowired
    private WordService wordService;

    @Autowired
    private WordCollectionService wordCollectionService;

    @GetMapping("/{id}")
    public JsonResponse<Object> word(@PathVariable Long id) {
        // 获取单词信息
        WordDetailVo wordDetailVO =
                JSONObject.parseObject(JSON.toJSONString(wordService.getWordDetail(id,getUid())), WordDetailVo.class);

        // 查询单词是否已收藏
        wordDetailVO.setIsFavorite(wordCollectionService.wordIsCollected(getUid(), id) ? 1 : 0);

        return JsonResponse.success(wordDetailVO);
    }

    /**
     * [L010]收藏单词
     * POST /learn/word/collection
     * 接口ID：50340453
     * 接口地址：https://www.apifox.cn/web/project/1916282/apis/api-50340453
     */
    @PostMapping("/collection")
    public JsonResponse<Object> collectWord(Long wordId) {
        wordCollectionService.collectWord(getUid(), wordId);
        return JsonResponse.success();
    }

    /**
     * [L010-1]取消收藏单词
     * DELETE /learn/word/collection
     * 接口ID：52836982
     * 接口地址：https://www.apifox.cn/web/project/1916282/apis/api-52836982
     */
    @DeleteMapping("/collection")
    public JsonResponse<Object> cancelCollectWord(Long wordId) {
        wordCollectionService.cancelCollectWord(getUid(), wordId);
        return JsonResponse.success();
    }

    /**
     * [L011]获取收藏夹单词列表
     * GET /learn/word/collection
     * 接口ID：50340672
     * 接口地址：https://www.apifox.cn/web/project/1916282/apis/api-50340672
     */
    @GetMapping("/collection")
    public JsonResponse<Object> getCollectionList() {
        return JsonResponse.success(wordCollectionService.getCollectionList(getUid()));
    }
}
